package org.csu.myblogger.myblogger.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.Getter;
import org.csu.myblogger.myblogger.commom.CommonResponce;
import org.csu.myblogger.myblogger.entity.Account;
import org.csu.myblogger.myblogger.entity.WebSignon;
import org.csu.myblogger.myblogger.mapper.AccountMapper;
import org.csu.myblogger.myblogger.mapper.WebSignonMapper;
import org.csu.myblogger.myblogger.service.service.AccountService;
import org.csu.myblogger.myblogger.vo.AccountVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Getter
@Service("accountService")
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private WebSignonMapper webSignonMapper;
    @Override
    public CommonResponce Userlogin(String username, String password) {
        String testpsd=webSignonMapper.selectById(username).getPassword();
        if (testpsd.equals(password)){
            return CommonResponce.CreatForSuccess("登录成功");
        }else {
            return CommonResponce.createForError(1,"密码错误");
        }
    }
    @Override
    public CommonResponce Userlogout(String username) {
        //清除token是在前端自行清除
        return CommonResponce.CreatForSuccess("退出成功");
    }

    @Override
    public CommonResponce Register(String username, AccountVo accountVo) {
        //判断是否已经存在account
        QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
        accountQueryWrapper.eq("username", username);
        int number = accountMapper.selectList(accountQueryWrapper).size();
        if (number != 0) {
            return CommonResponce.createForError(1, "用户名已经存在");
        }

        //accountvo赋值给account
        Account account = new Account();
        WebSignon webSignon = new WebSignon();
        BeanUtils.copyProperties(accountVo, account);// 一键复制同名属性
        BeanUtils.copyProperties(accountVo, webSignon);

        String username1 = null;
        String username2 = null;
        try {
            accountMapper.insert(account);
            webSignonMapper.insert(webSignon);
            username1 = account.getUsername();
            username2 = webSignon.getUsername();
        } catch (Exception e) {
            System.out.println(new Exception(e));
            return CommonResponce.createForError(500,"服务器出错，添加失败");
        }
        if (accountMapper.selectById(username1) != null && webSignonMapper.selectById(username2)!=null) {
            return CommonResponce.CreatForSuccess("注册成功");
        } else return CommonResponce.createForError(1, "注册失败");
    }

    @Override
    public CommonResponce UpdateUserInfo(AccountVo accountVo, String username) {
        //username是原来的账户名
        if (accountMapper.selectById(accountVo.getUsername())!=null){
            //用户名已经存在
            return CommonResponce.createForError(1,"用户名已存在");
        }
        Account account=new Account();
        BeanUtils.copyProperties(accountVo,account);

        LambdaUpdateWrapper<Account>accountLambdaUpdateWrapper=new LambdaUpdateWrapper<Account>
                ()
                .eq(Account::getUsername,username)
                .set(Account::getUsername,account.getUsername())
                .set(Account::getPassword,account.getPassword())
                .set(Account::getBirth,account.getBirth())
                .set(Account::getAvatar,account.getAvatar())
                .set(Account::getMale,account.getMale())
                .set(Account::getAge,account.getAge())
                .set(Account::getBirthplace,account.getBirthplace())
                .set(Account::getSignature,account.getSignature());
        LambdaUpdateWrapper<WebSignon>webSignonLambdaUpdateWrapper=new LambdaUpdateWrapper<WebSignon>
                ()
                .eq(WebSignon::getUsername,username)
                .set(WebSignon::getUsername,username)
                .set(WebSignon::getPassword,account.getPassword());
        try{
            webSignonMapper.update(webSignonLambdaUpdateWrapper);
            accountMapper.update(accountLambdaUpdateWrapper);
        }catch (RuntimeException e){
            System.out.println(new RuntimeException(e));
            return CommonResponce.createForError(500,"服务器出错，添加失败，控制台看详情");
        }
        return CommonResponce.CreatForSuccess("用户信息更新成功");
    }

    @Override
    public CommonResponce DeleteUser(String username) {
        QueryWrapper<Account>accountQueryWrapper=new QueryWrapper<Account>().eq("username",username);
        QueryWrapper<WebSignon>webSignonQueryWrapper=new QueryWrapper<WebSignon>().eq("username",username);

        try {
            accountMapper.delete(accountQueryWrapper);
            webSignonMapper.delete(webSignonQueryWrapper);
        }catch (RuntimeException e){
            System.out.println(new RuntimeException(e));
            return CommonResponce.createForError(500,"服务器出错，删除失败，控制台查看详情");
        }
        return CommonResponce.CreatForSuccess("注销成功");
    }

    @Override
    public CommonResponce<AccountVo> getAccountInfo(String username) {
        Account account = accountMapper.selectById(username);
        AccountVo accountVo = new AccountVo();
        BeanUtils.copyProperties(account,accountVo);
        if (account==null){
            return CommonResponce.createForError(1,"获取用户信息失败");
        }else return CommonResponce.CreatForSuccess("获取用户信息成功",accountVo);
    }
}
